package com.hghivln.erpwarehouse.system.controller;


import com.hghivln.erpwarehouse.system.common.lang.*;
import com.hghivln.erpwarehouse.system.entity.Menu;
import com.hghivln.erpwarehouse.system.entity.User;
import com.hghivln.erpwarehouse.system.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-06-19
 */
@RestController
@RequestMapping("api/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 加载所有菜单【顶部菜单和左边菜单】
     *
     * @return
     */
    @RequestMapping("loadIndexMenu")
    public Object loadIndexMenu() {
        // 获得当前登录用户 判断用户类型  权限
        ActiverUser activerUser = (ActiverUser) SecurityUtils.getSubject().getPrincipal();
        User user = activerUser.getUser();
        List<Menu> menulist = null;
        if (user.getType().equals(Constant.USER_TYPE_SUPER)) {
            // 用户类型为超级管理员
            menulist = menuService.queryAllMenuForList();
        } else {
            // 用户类型为普通用户
            menulist = menuService.queryMenuForListByUserId(user.getId());
        }

        ArrayList<TreeNode> treeNodes = new ArrayList<>();
        for (Menu menu : menulist) {
            Integer id = menu.getId();
            Integer pid = menu.getPid();
            String title = menu.getTitle();
            String icon = menu.getIcon();
            String href = menu.getHref();
            boolean spread = menu.getSpread().equals(Constant.SPREAD_TRUE);
            String target = menu.getTarget();
            String typecode = menu.getTypecode();
            treeNodes.add(new TreeNode(id, pid, title, icon, href, spread, target, typecode));
        }
        List<TreeNode> nodes = TreeNodeBuilder.build(treeNodes, 0);
        HashMap<String, Object> map = new HashMap<>();
        for (TreeNode node : nodes) {
            map.put(node.getTypecode(), node);
        }
        return map;
    }

    /**
     * 查询菜单和权限
     */
    @RequestMapping("loadAllMenu")
    public Object loadAllMenu(Menu menu) {
        return menuService.queryAllMenu(menu);
    }

    @RequestMapping("loadMenu")
    public DataGridlView loadAllMenu() {
        List<Menu> list = menuService.queryAllMenuForList();
        return new DataGridlView((long) list.size(), list);
    }

    /**
     * 查询最大排序码
     */
    @RequestMapping("queryMenuMaxOrderNum")
    public DataGridlView queryMenuMaxOrderNum() {
        Integer menuMaxOrderNum = menuService.queryMenuMaxOrderNum();
        return new DataGridlView(menuMaxOrderNum + 1);
    }

    /**
     * 添加菜单和权限
     */
    @RequestMapping("addMenu")
    @RequiresPermissions("menu:add")
    public Result addMenu(Menu menu) {
        try {
            menuService.addMenu(menu);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    /**
     * 修改菜单和权限
     */
    @RequestMapping("updateMenu")
    @RequiresPermissions("menu:update")
    public Result updateMenu(Menu menu) {
        try {
            Menu menu1 = menuService.getById(menu.getId());
            if (menu1.getPid().equals(0)) {
                return Result.UPDATE_REFULSE;
            } else {
                menuService.updateMenu(menu);
                return Result.UPDATE_SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @RequestMapping("updateMenuBtn")
    public Result updateMenuBtn(Menu menu) {
        Menu menu1 = menuService.getById(menu.getId());
        try {
            if (menu1.getPid().equals(0)) {
                return Result.UPDATE_REFULSE;
            } else {
                menuService.updateMenuBtn(menu);
                return Result.UPDATE_SUCCESS;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    /**
     * 返回当前菜单的子节点的数量
     */
    @RequestMapping("getMenuChildrenCountById")
    public Object getMenuChildrenCountById(Integer id) {
        return menuService.getMenuChildrenCountById(id);
    }

    /**
     * 删除菜单和权限
     */
    @RequestMapping("deleteMenu")
    @RequiresPermissions("menu:delete")
    public Result deleteMenu(Integer id) {
        try {
            menuService.removeById(id);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    @RequestMapping("getMenuById")
    public DataGridlView getMenuById(Integer id) {
        Menu menu = menuService.getById(id);
        return new DataGridlView(menu);
    }
}
